package com.hmall.item.config;

import com.hmall.item.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {

    /**
     * 创建 topic 交换机
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(MqConstants.ITEM_EXCHANGE, true, false);
    }

    /**
     * 创建用于添加和修改的队列
     *
     * @return
     */
    @Bean
    public Queue insertQueue() {
        return new Queue(MqConstants.ITEM_INSERT_QUEUE, true);
    }

    /**
     * 创建用于删除的队列
     *
     * @return
     */
    @Bean
    public Queue deleteQueue() {
        return new Queue(MqConstants.ITEM_DELETE_QUEUE, true);
    }

    /**
     * 将新增更新队列与交换机绑定，并指定RoutingKey
     *
     * @return
     */
    @Bean
    public Binding insertQueueBinding() {
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.ITEM_INSERT_KEY);
    }

    /**
     * 将删除队列与交换机绑定，并指定RoutingKey
     *
     * @return
     */
    @Bean
    public Binding deleteQueueBinding() {
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.ITEM_DELETE_KEY);
    }
}
